package com.qidi.ssh.service;

import com.qidi.ssh.bean.Customer;
import com.qidi.ssh.dao.CustomerDao;
import com.qidi.ssh.utils.PageBean;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-10-16-21:53
 */
@Transactional
public class CustomerServiceimpl implements CustomerService {
    private CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public void saveCustomer(Customer customer) {
        customerDao.save(customer);
    }

    @Override
    public PageBean<Customer> ListByPage(DetachedCriteria detachedCriteria, Integer pageSize, Integer pageNo) {
        PageBean<Customer> customers = new PageBean<>();
        customers.setPageNo(pageNo);
        customers.setPageSize(pageSize);
        Integer totalCounts=customerDao.findCount(detachedCriteria);
        customers.setTotalCount(totalCounts);
        Double num = Math.ceil(totalCounts.doubleValue()/pageSize);
        customers.setTotalPage(num.intValue());
        List<Customer> customerList=customerDao.findByPage(detachedCriteria,pageNo,pageSize);
        customers.setList(customerList);
        return customers;
    }

    @Override
    public void deleteCustomer(Long cust_id) {
        Customer customer = customerDao.findById(cust_id);
        if (customer.getCust_image()!=null){
            File file = new File(customer.getCust_image());
            if (file!=null){
                file.delete();
            }
        }
        customerDao.delete(customer);
    }

    @Override
    public Customer edit(Long cust_id) {
        return customerDao.findById(cust_id);
    }

    @Override
    public void UpdateCustomer(Customer customer) {
        customerDao.update(customer);
    }

    @Override
    public List<Customer> findAlls() {
        return customerDao.findAll();
    }

}
